import type { RequiredDeep } from 'type-fest'

import { ajax } from '@/utils'

import type { components } from '../schema'
import type { IDeleteReq, IExportRes } from '../type'

/**
 * 用户接口
 */
export const userApi = {
  /**
   * 获取用户分页
   */
  getUserPage(data: IGetUserPageReq): Promise<IGetUserPageRes> {
    return ajax.post('/system/user/page', data)
  },
  /**
   * 获取添加用户表单数据
   */
  getUserAddFormData(): Promise<IGetUserAddFormDataRes> {
    return ajax.get('/system/user/formAddData')
  },
  /**
   * 获取编辑用户表单数据
   */
  getUserEditFormData(id: string): Promise<IGetUserEditFormDataRes> {
    return ajax.get('/system/user/formEditData', { params: { id } })
  },
  /**
   * 添加用户
   */
  createUser(data: ICreateUserReq) {
    return ajax.post('/system/user/create', data)
  },
  /**
   * 更新用户
   */
  updateUser(id: string, data: IUpdateUserReq) {
    return ajax.put('/system/user/update', data, { params: { id } })
  },
  /**
   * 删除用户
   */
  deleteUser(data: IDeleteReq) {
    return ajax.delete('/system/user/delete', { data })
  },
  /**
   * 导出用户模板
   */
  importUserTemplate(data: FormData): Promise<IImportUserTemplate> {
    return ajax.post('/system/user/template/import', data)
  },
  /**
   * 导出用户模板
   */
  exportUserTemplate(): Promise<IExportRes> {
    return ajax.post('/system/user/template/export')
  },
  /**
   * 导出用户分页
   */
  exportUserPage(data: IGetUserPageReq): Promise<IExportRes> {
    return ajax.post('/system/user/page/export', data)
  },
  /**
   * 重置密码
   */
  resetPassword(id: string, data: IResetPasswordReq) {
    return ajax.put('/system/user/resetPassword', data, { params: { id } })
  },
  /**
   * 切换用户状态
   */
  toggleUserStatus(id: string) {
    return ajax.put('/system/user/toggleStatus', null, { params: { id } })
  }
}

export type IGetUserPageReq = components['schemas']['FindUserPageDTO']
export type IGetUserPageRes = components['schemas']['FindUserPageVO']['data']
export type IUserPageItem = IGetUserPageRes['list'][0]
export type IGetUserAddFormDataRes = RequiredDeep<
  components['schemas']['GetUserAddFormDataVO']['data']
>
export type IGetUserEditFormDataRes = RequiredDeep<
  components['schemas']['GetUserEditFormDataVO']['data']
>
export type ICreateUserReq = components['schemas']['CreateUserDTO']
export type IUpdateUserReq = components['schemas']['UpdateUserDTO']
export type IResetPasswordReq = components['schemas']['ResetPasswordDTO']
export type IImportUserTemplate =
  components['schemas']['ImportUserTemplateVO']['data']
